<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <el-table :data="catelist" style="width: 100%" stripe border>
        <el-table-column type="index" label="序号" width="180"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <!--在列中,通过插槽的形式,自定义这一列要渲染成什么样子  -->
        <el-table-column label="操作">
          <template v-slot:default="obj">
            <!-- v-slot:default="obj"  简写: 1 #default="obj" 2v-slot="obj"-->
            <!-- obj.row就是当前这一行的数据对象 -->
            <el-button type="primary" size="mini" @click="showEditDialog(obj.row)">修改</el-button>
            <el-button type="danger" size="mini" @click="remove(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog title="添加文章分类" :visible.sync="addVisible" width="30%" @closed="onAddClosed">
      <!-- 对话框 -->
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">添 加</el-button>
      </span>
    </el-dialog>
    <!-- 修改文章对话框 -->
    <el-dialog title="修改文章分类" :visible.sync="editVisible" width="35%">
      <!-- 修改文章对话框 -->
      <el-form :model="editForm" :rules="addRules" ref="editFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      // 文章列表的数据
      catelist: [],
      addVisible: false,
      editVisible: false,
      isShowEdit: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须为1~10位1非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须为1~15位字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.initArtCate()
  },
  methods: {
    // 初始化文章列表数据
    async initArtCate() {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code !== 0) return this.message.error('获取文章列表数据失败')
      this.catelist = res.data
    },
    // 关闭添加对话框数据重置
    onAddClosed() {
      this.$refs.addRef.resetFields()
    },
    // 添加文章
    addCate() {
      // 预验证
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error('添加分类失败')
        this.$message.success('添加分类成功')
        this.addVisible = false
        this.initArtCate()
      })
    },
    // 修改文章
    showEditDialog(row) {
      if (row.id === 1 || row.id === 2) {
        if (this.isShowEdit) return
        this.isShowEdit = true
        this.$message({
          message: '管理员不允许修改这个分类',
          type: 'error',
          onClose: () => {
            this.isShowEdit = false
          }
        })
        return
      }
      // 把点击行的数据对象,直接赋值给editForm
      this.editForm = Object.assign({}, row)
      // 展示修改分类对话框
      this.editVisible = true
    },
    editCate() {
      // 预验证
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error('修改失败')
        this.$message.success('更新成功')
        this.editVisible = false
        this.initArtCate()
      })
    },
    async remove(id) {
      if (id === 1 || id === 2) {
        if (this.isShowEdit) return
        this.isShowEdit = true
        return this.$message({
          message: '管理员不允许删除这个分类',
          type: 'error',
          onClose: () => {
            this.isShowEdit = false
          }
        })
      }
      const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result === 'cancel') return
      const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
      if (res.code !== 0) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.initArtCate()
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
